package com.gjszn.kh.controller.page;


import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import com.gjszn.kh.context.MemberContext;
import com.gjszn.kh.context.MemberRCodeContext;
import com.gjszn.kh.model.User;
import com.gjszn.kh.service.UserService;
import com.gjszn.kh.utils.ImageUtil;


/**    
* @Title: SelectMemberController.java  
* @Package com.gjszn.kh.controller.page    
* @author jys@nx-yt.com    
* @date 2016年1月3日 下午3:46:28  
* @version V1.0    
* @Description: TODO(用一句话描述该文件做什么)
********************************************/
@Controller
public class SelectMemberController {
	private static Logger log = LogManager.getRootLogger();
	
	@Inject
	private UserService userService;
	
	@RequestMapping("/input/rcode")
	public String toInputRCodeView(HttpServletRequest request){
		String baseUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ request.getContextPath() + "/register/view";
		request.getSession().setAttribute("base_url", baseUrl);
		request.setAttribute("memberMap", MemberContext.getMemberMap());
		return "rcode"; 
	}
	
	/**
	 * 
	 * @param id
	 * @param response void
	 */
	@RequestMapping("/get/number")
	public void drawNumber(int id, HttpServletResponse response){
		User user = userService.getById(id);
		String number = "";
		if(user != null){
			number = user.getNumber();
		}
		ImageUtil.drawNumber(205, 35, number, response);
	}
	
	/**
	 * 跳转到选择会员单位的界面
	 * @param request
	 * @return String
	@RequestMapping("/select/members")
	public String toSelectMemberView(HttpServletRequest request){
		request.setAttribute("memberMap", MemberContext.getMemberMap());
		return "select_member"; 
	}
	*/
	/**
	 * 客户选择了会员单位之后提交到此地址，之后转向客户风险提示界面
	 * @param member
	 * @param session
	 * @return String
	@RequestMapping(value="/select/members/sumbit", method=RequestMethod.POST)
	public String toRiskView(String member, String rcode, HttpSession session){
		String memberNo = Base64Util.encode(member); 
		session.setAttribute("member", memberNo);
		log.info("member:" + memberNo);
		return "redirect:/selected/" + memberNo; 
	}
	 */
	/**
	 * 客户直接通过已经选择的了会员打开地址，则直接转向风险提示界面
	 * 此处的地址可以携带一个code地址，作为推荐码
	 * @param member
	 * @param request
	 * @return String
	 */
	@RequestMapping(value="/select/sumbit", method=RequestMethod.POST)
	public String toRiskView(String rcode, HttpServletRequest request, RedirectAttributesModelMap redirectAttributesModelMap){
		boolean rcode_flag = (rcode == null) || "".equals(rcode) || (!MemberRCodeContext.isInclude(rcode));
		if(rcode_flag){
			redirectAttributesModelMap.addFlashAttribute("errorMsg", "您填写的推荐码不正确!");
			return "redirect:/input/rcode"; 
		}
		String member = rcode.substring(0, 3);
		String code = rcode.substring(3);
		log.info("rcode:" + rcode);
		log.info("code:" + code);
		log.info("member:" + member);
		String memberName = MemberContext.getByKey(member).getName();
		log.info("memberName:" + memberName);
		//request.getSession().setAttribute("memberName", memberName);
		request.getSession().setAttribute("member", MemberContext.getByKey(member));
		request.getSession().setAttribute("rcode", rcode);
		return "redirect:/register/step/1"; 
	}
	
	/**
	 * 客户直接通过已经选择的了会员打开地址，则直接转向风险提示界面
	 * 此处的地址可以携带一个code地址，作为推荐码
	 * @param member
	 * @param request
	 * @return String
	@RequestMapping(value="/selected/sumbit", method=RequestMethod.POST)
	public String toRiskViewBy2WM(String code, HttpServletRequest request){
		log.info("执行/selected/{member}。。。。。。。。。。。。。。。。。1");
//		if(member==null || "".equals(member)){
//			return "redirect:/select/members";
//		}
//		log.info("执行/selected/{member}。。。。。。。。。。。。。。。。。2");
//		String memberNo = Base64Util.decode(member);
//		String codeStr = Base64Util.decode(rcode);
//		if(MemberContext.getByKey(memberNo) == null){
//			return "redirect:/select/members";
//		}
//		log.info("执行/selected/{member}。。。。。。。。。。。。。。。。。3");
//		request.getSession().setAttribute("member", member);
//		request.getSession().setAttribute("rcode", codeStr);
		return "risk"; 
	}
	*/
}

